//
//  MVVM_tableViewController.m
//  MVC|MVP|MVVM
//
//  Created by Guo Rui on 2017/11/15.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "MVVM_tableViewController.h"
#import "Service.h"
#import "ViewModel.h"
#import "MVVM_TableViewCell.h"

@interface MVVM_tableViewController ()
@property (strong, nonatomic) NSMutableArray <ViewModel *> *vms;
@end

@implementation MVVM_tableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MVVM_TableViewCell class]) bundle:nil] forCellReuseIdentifier:@"ID"];
}

- (NSMutableArray *)vms
{
    if (!_vms) {
        _vms = [NSMutableArray arrayWithCapacity:0];
    }
    return _vms;
}

- (void)loadData
{
    __weak typeof(self) _self = self;
    [Service.sharedInstance fetchTimelineWithCompletion:^(NSArray *array) {
        for (TimelineModel *model in array) {
            ViewModel *vm = [[ViewModel alloc] initWithModel:model];
            [_self.vms addObject:vm];
        }
        
        [_self.tableView reloadData];
    }];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.vms.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MVVM_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID" forIndexPath:indexPath];
    
    ViewModel *vm = [self.vms objectAtIndex:indexPath.row];
    cell.vm = vm;
    
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
